//引入并创建createSlice
import {
    createSlice,
    createAsyncThunk
} from '@reduxjs/toolkit'

//引入axios请求接口
import {
    MovieRequest
  } from '../../api'

  //我们使用createAsyncThunk方法来创建异步action

  export  const getMovieAction = createAsyncThunk('movie/getMovieList',async ()=>{
    //在函数中执行异步请求
    const result =await MovieRequest()
    console.log(result, "result");
    //在这个函数中return的值，就是在thunk中间件中再次disptch的值
    return result.data.data.list

  })


  const movieSlice = createSlice({
    //配置type和初始数据
    name:'movie',
    initialState:{
        movieList:[]
    },
    //异步处理不写在reducer中，因为会生成action，需要额外书写
    reducers:{

    },

    extraReducers:{
        [getMovieAction.fulfilled](prev, action) {
            console.log("异步action成功--getMovieAction");
            console.log(prev.movieList, action);
            prev.movieList = action.payload

          },
          [getMovieAction.rejected]() {
            console.log("异步action失败--getMovieAction");
          },
          [getMovieAction.pending]() {
            console.log("异步action正在进行中--getMovieAction");
          },
    }

  })

  export default movieSlice.reducer;